package com.invite.me.ui.registeractivity;

import android.content.Intent;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.presenter.PasswordRegisterPresenter;
import com.invite.me.beans.res.MimadengluBean;
import com.invite.me.ui.home.HomeActivity;
import com.invite.me.util.PhoneFormatCheckUtils;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.util.ToastUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;

public class PasswordRegisterActivity extends BaseActivity implements View.OnClickListener,Contract.BaseView{
    PasswordRegisterPresenter passwordRegisterPresenter;
    private boolean mimakanjian=false;
    private EditText yanzhengma;
    private EditText zhanghaophone;


    @Override
    protected void initView() {
        passwordRegisterPresenter=new PasswordRegisterPresenter(this);
        TextView zhanghaodenglu= findViewById(R.id.zhanghaodenglu);
        TextView enroll= findViewById(R.id.enroll);
        TextView gorgetpawwword= findViewById(R.id.gorgetpawwword);

        ImageView chahao= findViewById(R.id.chahao);
        ImageView yanzhengmasend= findViewById(R.id.yanzhengmasend);
        yanzhengma = findViewById(R.id.yanzhengma);
        zhanghaophone = findViewById(R.id.zhanghaophone);
        TextView pawasswordregister= findViewById(R.id.pawasswordregister);
        chahao.setOnClickListener(this);
        pawasswordregister.setOnClickListener(this);
        yanzhengmasend.setOnClickListener(this);
        zhanghaodenglu.setOnClickListener(this);
        enroll.setOnClickListener(this);
        gorgetpawwword.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.chahao:

                finish();

                break;
            case R.id.pawasswordregister:
                Intent pwr=new Intent(PasswordRegisterActivity.this,RegisterActivity.class);
                startActivity(pwr);
                finish();
                break;
            case R.id.yanzhengmasend:
                if(mimakanjian){
                    mimakanjian=false;//隐藏
                    yanzhengma.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else{
                    mimakanjian=true;//可见
                    yanzhengma.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }

                break;
            case R.id.zhanghaodenglu:
                String phone = zhanghaophone.getText().toString();
                String mima = yanzhengma.getText().toString();
                boolean phonedl = PhoneFormatCheckUtils.isPhoneLegal(phone);
                if(phonedl){
                    if(!TextUtils.isEmpty(mima)&&mima.length()>=6){
                        passwordRegisterPresenter.yanzhengma(phone,mima);
                    }else {
                        ToastUtils.show(PasswordRegisterActivity.this, "请输入正确密码", Toast.LENGTH_SHORT);
                    }

                }else {
                    ToastUtils.show(PasswordRegisterActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);
                }





                break;
            case R.id.enroll:
                Intent rnb=new Intent(PasswordRegisterActivity.this,RegisterNumberActivity.class);
                startActivity(rnb);
                finish();
                break;
            case R.id.gorgetpawwword:
                Intent chongzhi=new Intent(PasswordRegisterActivity.this,RegisterCsmmActivity.class);
                startActivity(chongzhi);

                break;

        }

    }


    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_passwordregister;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (passwordRegisterPresenter instanceof Contract.BasePresenter) {
            this.passwordRegisterPresenter = (PasswordRegisterPresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o) {
        if(o instanceof MimadengluBean){
            MimadengluBean mimadengluBean= (MimadengluBean) o;
            if(mimadengluBean.getCode()==1000){
                String userId = mimadengluBean.getData().getUserId();
                String nickname = mimadengluBean.getData().getNickname();
                String headerImgPath = mimadengluBean.getData().getHeaderImgPath();
                SspPrefUtils.setString(Constants.SSPTOKEN,userId);
                if(!TextUtils.isEmpty(nickname)){
                    SspPrefUtils.setString(Constants.NAKNAME,nickname);
                }
                if(!TextUtils.isEmpty(headerImgPath)){
                    SspPrefUtils.setString(Constants.TOUXIANG,headerImgPath);
                }


                Intent pwr=new Intent(PasswordRegisterActivity.this, HomeActivity.class);
                startActivity(pwr);
                finish();


            }else {
                ToastUtils.show(PasswordRegisterActivity.this,mimadengluBean.getMessage());
            }

        }


    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }
}